<?php

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

$setting=$this->setting;
$tabs=array(
	array('op'=>'list','title'=>'售后群管理'),
	array('op'=>'edit','title'=>'添加/编辑售后群'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	$params['status']=intval($_RQ['status']);
	$return['params']['status']=$_RQ['status'];
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['xuesheng'])){
		$classid=pdo_getcolumn('jy_yxw_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'title'=>$_RQ['xuesheng']),'classid');
		$classid=$classid?:-1;
		$params['id']=$classid;
		$return['params']['xuesheng']=$_RQ['xuesheng'];
	}
    $orderby='id desc';
	$total='';
	$return['list']=$this->getall('class',$params,array(),'',$orderby);
	foreach($return['list'] as &$item){
		if($item['open_time'] && $item['open_time']<time() && !$item['status']){
			$this->update('class',array('status'=>1),array('id'=>$item['id']));
			$this->model('xuesheng')->upclassxs_status($item['id'],1);
		}
		$item['open_time']=date('Y-m-d',$item['open_time']);
		$item['set']='/jiaoxue.class/set/id='.$item['id'];
	}
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'售后群名称','type'=>'string'),
			array('name'=>'workerid','title'=>'售后主管','type'=>'select','options'=>'workers'),
			array('name'=>'workerid9','title'=>'售后助理售后','type'=>'select','options'=>'workers9'),
			array('name'=>'workerid10','title'=>'正课售后','type'=>'select','options'=>'workers10'),
			array('name'=>'kechengid','title'=>'绑定使用','type'=>'select','options'=>'kechengs'),
			array('title'=>'门牌号/容纳人数','type'=>'img_string',
			'data'=>array(
				array('title'=>'牌号：','name'=>'menpai'),
				array('title'=>'容纳人数：','name'=>'rongnum')
				)
			),
			array('title'=>'用户人数/休转退','type'=>'img_string',
			'data'=>array(
				array('title'=>'用户人数：','name'=>'xueshengnum'),
				array('title'=>'实际人数：','name'=>'rongnum'),
				array('title'=>'休转退：','name'=>'rongnum')
				)
			),
			array('name'=>'keshi','title'=>'课时','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radiomodal','options'=>'status','action'=>'set'),
			array('name'=>'open_time','title'=>'开班时间','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','title'=>'前往管理售后群','action'=>'/xuesheng.danganlist/fromclass/status=all'),
			array('action'=>'jy_yxw/jiaoxue.class_kc/','title'=>'使用','type'=>'href'),
			array('action'=>'jy_yxw/jiaoxue.class/edit','title'=>'编辑','type'=>'href'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'售后群名称','name'=>'title','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'用户名称','name'=>'xuesheng','help'=>''),
			//array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status'),
		),
	);
	//$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['workers']=$this->getall('worker',array('role'=>11,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['kechengs']=$this->getall('kecheng',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['workers9']=$this->getall('worker',array('role'=>9,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['workers10']=$this->getall('worker',array('role'=>10,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['status']=$this->class_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='status2'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$this->update('class',array('status'=>2),array('id'=>$id,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']));
	$this->update('xuesheng',array('status'=>13),array('classid'=>$id,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$update=array('status'=>$post['status']);
		pdo_update('jy_yxw_class',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if($post['status']==2){
			$this->model('xuesheng')->upclassxs_status($id,2);
		}elseif($post['status']==1){
			$this->model('xuesheng')->upclassxs_status($id,1);
		}elseif($post['status']==0){
			$this->model('xuesheng')->upclassxs_status($id,0);
		}
		exi('操作成功！','','/jiaoxue.class/');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择售后群','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置状态',
	);
	$htmls['edit']=array(
		array('type'=>'radio','title'=>'设置状态','name'=>'status','options'=>'status'),
	);
	$return['status']=$this->class_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('class',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array(
			'title'=>'',
		);
	}
	$item['open_time'] =$item['open_time']?date('Y/m/d H:i',$item['open_time']):'';
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'售后群设置',
		'edit'=>array(
			array('name'=>'title','title'=>'售后群名称','type'=>'string'),
			array('name'=>'desc','title'=>'售后群描述','type'=>'string'),
			//array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'workerid','title'=>'售后主管','type'=>'select','options'=>'workers'),
			array('name'=>'workerid12','title'=>'售后反馈','type'=>'select','options'=>'workers12'),
			array('name'=>'workerid9','title'=>'售后助理售后','type'=>'select','options'=>'workers9'),
			array('name'=>'workerid10','title'=>'正课售后','type'=>'select','options'=>'workers10'),
			array('name'=>'kechengid','title'=>'绑定使用','type'=>'select','options'=>'kechengs'),
			array('name'=>'keshi','title'=>'课时','type'=>'string'),
			array('name'=>'menpai','title'=>'售后群门牌号','type'=>'string'),
			array('name'=>'rongnum','title'=>'可容纳人数','type'=>'string'),
			array('name'=>'xueshengnum','title'=>'用户人数','type'=>'string'),
			array('name'=>'open_time','title'=>'开班时间','type'=>'time','time_type'=>'day'),
		),
	);
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['workers']=$this->getall('worker',array('role'=>11,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers12']=$this->getall('worker',array('role'=>12,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers9']=$this->getall('worker',array('role'=>9,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['workers10']=$this->getall('worker',array('role'=>10,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['kechengs']=$this->getall('kecheng',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');		
	$return['status']=$this->class_status;
	$return['item']=$item;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请填写售后群名称！','error');
	}
	if(!$postdata['workerid']){
		exi('请选择售后主管！','error');
	}
	if(!$postdata['kechengid']){
		exi('请选择使用！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'workerid'=>intval($postdata['workerid']),
		'workerid12'=>intval($postdata['workerid12']),
		'workerid9'=>intval($postdata['workerid9']),
		'workerid10'=>intval($postdata['workerid10']),
		'kechengid'=>intval($postdata['kechengid']),
		'keshi'=>intval($postdata['keshi']),
		'image'=>trim($postdata['image']),
		'desc'=>trim($postdata['desc']),
		'xueshengnum'=>intval($postdata['xueshengnum']),
		'status'=>intval($postdata['status']),
		'menpai'=>trim($postdata['menpai']),
		'rongnum'=>trim($postdata['rongnum']),
		'open_time'=>strtotime($postdata['open_time']),
	);
	$id=intval($postdata['id']);
	if($id){
		$this->update('class',$data,array('id'=>$id));
		exi('设置成功！','','/jiaoxue.class/');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['storeid']=$_SESSION['storeid'];
		$data['createtime']=time();
		$this->insert('class',$data);
		exi('添加成功！','','/jiaoxue.class/');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('class',array('storeid'=>$_SESSION['storeid'],'id'=>$id));	
	}
	if(!$id){
		exi('默认等级不能删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('class',array('storeid'=>$_SESSION['storeid'],'id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>